<template>
  <div class="app-container">
    <el-form :model="searchForm" label-width="100px" @keyup.enter.native="search()">
      <el-row>
        <el-col class="datePicker">
          <el-form-item label="合同金额" size="small">
            <el-input type="number" v-model.number="searchForm.contractAmountStart">
              <span slot="suffix">万元</span>
            </el-input>
            -
            <el-input type="number" v-model.number="searchForm.contractAmountEnd">
              <span slot="suffix">万元</span>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col class="other">
          <el-form-item label="合同排序" size="small">
            <el-radio-group v-model="searchForm.orderByContractAmount" @change="search">
              <el-radio :label="1">降序</el-radio>
              <el-radio :label="2">升序</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col class="other">
          <el-form-item label="合同编号" size="small">
            <el-input v-model="searchForm.contractCode"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col class="datePicker">
          <el-form-item label="造价金额" size="small">
            <el-input type="number" v-model.number="searchForm.projectCostStart">
              <span slot="suffix">万元</span>
            </el-input>
            -
            <el-input type="number" v-model.number="searchForm.projectCostEnd">
              <span slot="suffix">万元</span>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col class="other">
          <el-form-item label="造价排序" size="small">
            <el-radio-group v-model="searchForm.orderByProjectCost" @change="search">
              <el-radio :label="1">降序</el-radio>
              <el-radio :label="2">升序</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col class="other">
          <el-form-item label="项目负责人" size="small">
            <el-input v-model="searchForm.projectLeader"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col class="datePicker">
          <el-form-item label="项目规模" size="small">
            <el-input type="number" v-model.number="searchForm.projectScaleOne">
            </el-input>
            -
            <el-input type="number" v-model.number="searchForm.projectScaleTwo">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col class="other">
          <el-form-item label="项目名称" size="small">
            <el-input v-model="searchForm.projectName"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="other">
          <el-form-item label="地区" size="small">
            <el-input v-model="searchForm.address"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col class="professions">
          <el-form-item label="所属专业" size="small">
            <el-select v-model="searchForm.professions" placeholder="请选择">
              <el-option value="" label="全部"></el-option>
              <el-option
                v-for="item in parentProfessionList"
                :key="item.professionId"
                :label="item.professionName"
                :value="item.professionName">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col class="professions">
          <el-form-item label="是否投标项目" size="small" class="item-inline">
            <el-select v-model="searchForm.isTenderBook" placeholder="请选择">
              <el-option value="" label="全部"></el-option>
              <el-option
                v-for="item in global.isTenderBookList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col class="searchBtn">
          <el-form-item size="small">
            <el-button type="primary" icon="el-icon-search" @click="search()">搜索</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="table-btn">
      <!--<el-button type="primary" size="small" icon="el-icon-plus" @click="addTeam()">添加</el-button>-->
      <el-dialog :title="dialogTitle" :visible.sync="outerVisible" width="440px">
        <el-form ref="teamRow" :rules="rules" :model="teamRow" label-width="100px">
          <el-form-item prop="projectName" label="项目名称" size="small">
            <el-input type="text" v-model="teamRow.projectName"></el-input>
          </el-form-item>
          <el-form-item prop="address" label="地点" size="small">
            <el-input type="text" v-model="teamRow.address"></el-input>
          </el-form-item>
          <el-form-item prop="time" label="截止日期" size="small">
            <el-date-picker
              v-model="teamRow.time"
              type="date"
              value-format="timestamp"
              placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="费率" size="small">
            <el-input type="number" v-model.number="teamRow.rate">
              <span slot="suffix">%</span>
            </el-input>
          </el-form-item>
          <el-form-item prop="storageTeam" label="入库团队" size="small">
            <el-input type="text" v-model.number="teamRow.storageTeam"></el-input>
          </el-form-item>
          <el-form-item label="备注说明" size="small">
            <el-input type="textarea" v-model="teamRow.remark"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="warning" size="small"  @click="submitForm('teamRow')" v-no-more-click>确定</el-button>
          <el-button size="small" @click="outerVisible = false">取消</el-button>
        </div>
      </el-dialog>
      <!--<el-button size="small" icon="el-icon-upload2">导出</el-button>-->
    </div>
    <my-table
      :data="tableData"
      :col-configs="roleType ===0 || roleType ===1 || roleType ===5 ? colConfigs : noConfigs"
      v-loading="listLoading">
      <el-table-column
        slot="opt"
        fixed="right"
        label="操作"
        width="120">
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="editTeam(scope.row.storageId)"
            type="text"
            size="small">
            修改
          </el-button>
          <el-popover
            placement="top"
            width="160"
            :ref="`popover-${scope.$index}`"
          >
            <p>确定删除这条数据吗?</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[`popover-${scope.$index}`].doClose()">取消</el-button>
              <el-button type="primary" size="mini" @click.native.prevent="deleteRow(scope)">确定</el-button>
            </div>
            <el-button type="text" size="small" slot="reference" v-popover:popover-${scope.$index}>删除</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </my-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
  import { getList, getParentProfessionList } from '@/api/results1'
  import myTable from '@/components/myTable'
  import global from '@/components/global'

  import { AreaSelect } from 'vue-area-linkage'
  import { pca } from 'area-data'
  import { formatDate } from '@/utils/index'

  const filePath = {
    props: ['colConfig'],
    methods: {
      openVisible(label, src) {
        const msg = {
          label: label,
          src: src
        }
        this.$emit('openImgVisible', msg)
      }
    },
    template: `
  <el-table-column :label="colConfig.label" :min-width="colConfig.minWidth">
      <span slot-scope="scope">
        <div v-if="colConfig.type==='img'">
          <a v-if="scope.row[colConfig.prop]" @click="openVisible(colConfig.label,scope.row[colConfig.prop])">查看
          </a>
        </div>
        <div v-else>
          <a v-if="scope.row[colConfig.prop]" target="_blank" :href="GLOBAL.filePathUrl1+ encodeURI(scope.row[colConfig.prop])">下载</a>
        </div>
      </span>
  </el-table-column>
  `
  }

  export default {
    components: {
      myTable,
      AreaSelect
    },
    data() {
      return {
        colConfigs: [
          { prop: 'projectName', label: '项目名称', minWidth: '40%', fixed: true },
          { prop: 'address', label: '地区', minWidth: '20%' },
          { prop: 'projectLeader', label: '项目负责人', minWidth: '15%' },
          { prop: 'professionName', label: '主导专业', minWidth: '10%' },
          { prop: 'contractAmount', label: '合同金额(万元)', width: '110' },
          { prop: 'projectScale', label: '项目规模', minWidth: '10%' },
          { prop: 'projectCost', label: '造价(万元)', width: '100' },
          { prop: 'contractFile', label: '合同', minWidth: '10%', component: filePath }
        ],
        noConfigs: [
          { prop: 'projectName', label: '项目名称', minWidth: '40%', fixed: true },
          { prop: 'address', label: '地区', minWidth: '20%' },
          { prop: 'projectLeader', label: '项目负责人', minWidth: '15%' },
          { prop: 'professionName', label: '主导专业', minWidth: '10%' },
          { prop: 'contractAmount', label: '合同金额(万元)', width: '110' },
          { prop: 'projectScale', label: '项目规模', minWidth: '10%' },
          { prop: 'projectCost', label: '造价(万元)', width: '100' }
        ],
        searchForm: {},
        searchFormList: {},
        professions: [],
        parentProfessionList: [],
        typeNames: [],
        listLoading: true,
        dialogTitle: '新增入库信息',
        dialogType: 0,
        teamRow: {},
        teamRowClear: {},
        tableData: [],
        pageNum: 1,
        pageSize: 10,
        total: 1,
        outerVisible: false,
        pca: pca,
        rules: {
          projectName: [
            { required: true, message: '请输入地点', trigger: 'change' }
          ],
          address: [
            { required: true, message: '请输入地点', trigger: 'change' }
          ],
          time: [
            { required: true, message: '请选择截止日期', trigger: 'change' }
          ],
          rate: [
            { required: true, message: '请输入费率', trigger: 'change' }
          ],
          storageTeam: [
            { required: true, message: '请输入入库团队', trigger: 'change' }
          ]
        },
        visible: false
      }
    },
    created() {
      this.teamRowClear = JSON.parse(JSON.stringify(this.teamRow))
      this.fetchData()
      this.getParentProfessionList()
    },
    computed: {
      global() {
        return global
      },
      roleType() {
        return this.$store.state.user.roleType
      }
    },
    methods: {
      fetchData() {
        this.searchFormList.pageNum = this.pageNum
        this.searchFormList.pageSize = this.pageSize
        this.listLoading = true
        getList(this.searchFormList).then(response => {
          this.tableData = response.responseBody.list
          this.total = response.responseBody.total
          this.listLoading = false
        })
      },
      getParentProfessionList() {
        getParentProfessionList().then(response => {
          this.parentProfessionList = response.responseBody
        })
      },
      search() {
        this.pageNum = 1
        if (this.searchForm.projectScaleOne && this.searchForm.projectScaleTwo) {
          this.searchFormList = JSON.parse(JSON.stringify(this.searchForm))
        } else {
          this.searchFormList = JSON.parse(JSON.stringify(this.searchForm))
          if (this.searchForm.projectScaleOne) {
            this.searchFormList.projectScale = this.searchForm.projectScaleOne
          } else if (this.searchForm.projectScaleTwo) {
            this.searchFormList.projectScale = this.searchForm.projectScaleTwo
          }
          delete this.searchFormList.projectScaleOne
          delete this.searchFormList.projectScaleTwo
        }
        this.fetchData()
      },
      addTeam() {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.outerVisible = true
        this.dialogTitle = '新增入库信息'
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        this.dialogType = 1
      },
      editTeam(storageId) {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.outerVisible = true
        this.dialogTitle = '修改入库信息'
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        this.dialogType = 0
        const params = {
          storageId: storageId
        }
        selectStorageInfo(params).then(response => {
          this.teamRow = response.responseBody
        })
      },
      submitForm(formName) {
        const params = {
          address: this.teamRow.address,
          time: this.teamRow.time,
          projectName: this.teamRow.projectName,
          rate: this.teamRow.rate,
          storageTeam: this.teamRow.storageTeam,
          remark: this.teamRow.remark ? this.teamRow.remark : ''
        }
        if (!this.dialogType) {
          params.storageId = this.teamRow.storageId
        }
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.dialogType) {
              insertStorageInfo(params).then(response => {
                this.outerVisible = false
                this.fetchData()
              })
            } else {
              updateStorageInfo(params).then(response => {
                this.outerVisible = false
                this.fetchData()
              })
            }
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      deleteRow(scope) {
        console.log(this.$refs[`popover-${scope.$index}`])
        this.$refs[`popover-${scope.$index}`].doClose()
        const params = {
          storageId: scope.row.storageId,
          isDel: 1
        }
        delStorageInfo(params).then(response => {
          this.fetchData()
        })
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.pageNum = val
        this.fetchData()
      },
      addressChange() {
        this.teamRow.provinceId = this.teamRow.addressId[0]
        this.teamRow.cityId = this.teamRow.addressId[1]
        this.teamRow.address = this.pca[86][this.teamRow.addressId[0]] + this.pca[this.teamRow.addressId[0]][this.teamRow.addressId[1]]
      }
    },
    mounted() {
      console.log(this.pca)
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .app-container {
    .datePicker {
      .el-input {
        width: auto;
      }
    }
    .el-pagination {
      margin-top: 20px;
    }
    .table-btn {
      margin-bottom: 10px;
      & > button {
        margin: 0 20px;
      }
      .el-dialog {
        textarea {
          margin-top: 10px;
        }
        .el-checkbox {
          margin-right: 20px;
        }
        .el-checkbox + .el-checkbox {
          margin-left: 0;
        }
      }
    }
  }

</style>


<style rel="stylesheet/scss" lang="scss">
  .el-input__icon {
    line-height: 100%;
  }

  .el-form-item__content {
    line-height: 33px;
  }

  .area-select .area-selected-trigger {
    padding: 0 10px;
  }

  a, a:focus, a:hover {
    color: #409EFF;
  }

  /*.el-table__body tr.hover-row>td {
    background-color: #0000ff !important;
  }

  .el-table__body tr.current-row>td {
    background-color: #0000ff !important;
  }*/
</style>
